package com.aaron.kjviewutils.health;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.widget.CardView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.aaron.kjviewutils.R;
import com.aaron.kjviewutils.retrofit.RetrofitUtil;
import com.aaron.kjviewutils.spinkit.ProgressPopupWindow;
import com.github.ybq.android.spinkit.Style;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

/**
 * 作者：Aaron on 2017/2/20 17:11
 * 邮箱：kaijin.fang@edianniu.com
 */
public class HealthFragment extends Fragment {

    @Bind(R.id.healthTag)
    TextView healthTag;
    @Bind(R.id.healthConOutL)
    NestedScrollView healthConOutL;
    @Bind(R.id.healthConL)
    LinearLayout healthConL;

    private HealthActivity rootAty;
    private View rootView;
    private Retrofit retrofit;
    private Call<HealthBean> call;
    private HealthService healthService;
    private ProgressPopupWindow progress;


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        rootAty = (HealthActivity) getActivity();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.frgment_health, container, false);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initContent();
    }

    private void initContent()
    {
        if(null!=rootAty.progress)
        {
            progress = rootAty.progress;
        }
        else
        {
            progress = new ProgressPopupWindow(rootAty, rootAty);
        }
        progress.showProgress(Style.WAVE);
        progress.showAtLocation(healthTag, Gravity.CENTER, 0, 0);
        Bundle bundle = getArguments();
        if (bundle != null)
        {
            final String foot = bundle.getString("foot");
            if(null==rootAty.retrofit)
            {
                retrofit = RetrofitUtil.obtainRetrofit();
            }
            else
            {
                retrofit = rootAty.retrofit;
                healthService = retrofit.create(HealthService.class);
                call = healthService.obtainHealth(foot);
                call.enqueue(new Callback<HealthBean>() {
                    @Override
                    public void onResponse(Call<HealthBean> call, Response<HealthBean> response) {
                        if(null!=progress)
                        {
                            progress.dismissView();
                        }
                        HealthBean health = response.body();
                        if("200".equals(health.retCode))
                        {
                            if(null!=health.result)
                            {
                                healthTag.setText(foot+"健康小知识："+health.result.page+"页，共"+health.result.total+"条");
                                List<HealthBean.HealthInnerBean> healthInners = health.result.list;
                                if(null!=healthInners&&!healthInners.isEmpty())
                                {
                                    //动态加载数据
                                    healthConL.removeAllViews();
                                    for(HealthBean.HealthInnerBean healthInner:healthInners)
                                    {
                                        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup
                                                .LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                                        CardView healthCardL = (CardView) LayoutInflater.from(rootAty).inflate(R.layout.item_health, null);
                                        TextView healthTitle = (TextView) healthCardL.findViewById(R.id.healthTitle);
                                        TextView healthTag = (TextView) healthCardL.findViewById(R.id.healthTag);
                                        healthTitle.setText(healthInner.title);
                                        healthTag.setText(healthInner.content);
                                        healthCardL.setLayoutParams(lp);
                                        healthConL.addView(healthCardL);
                                    }
                                }
                            }
                            else
                            {
                                healthConOutL.setVisibility(View.GONE);
                                healthTag.setText("获取健康知识失败，请重试");
                            }
                        }
                        else
                        {
                            healthConOutL.setVisibility(View.GONE);
                            healthTag.setText(health.msg);
                        }
                    }

                    @Override
                    public void onFailure(Call<HealthBean> call, Throwable t) {
                        if(null!=progress)
                        {
                            progress.dismissView();
                        }
                        healthConOutL.setVisibility(View.GONE);
                        healthTag.setText("获取健康知识失败，请重试");
                    }
                });
            }

        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if(null!=call)
        {
            call.cancel();
        }
        if(null!=retrofit)
        {
            retrofit = null;
        }
        if(null!=progress)
        {
            progress.dismissView();
        }
        ButterKnife.unbind(this);
    }

    @Override
    public void onDetach() {
        super.onDetach();
        rootAty = null;
    }
}
